/*
 * ksData.cpp
 *
 *  Created on: Aug 5, 2011
 *      Author: wang
 */

#include "ksData.h"
#include <cstddef>
#include <iostream>
#include <fstream>
#include "ksDefines.h"

using namespace std;
DATA dataFromFile(const char* path)
{
    DATA data = { NULL, 0 };
    ifstream stream;
    stream.open(path, ifstream::in | ifstream::binary);
    if (stream.good())
    {
        stream.seekg(0, ifstream::end);
        unsigned int length = stream.tellg();

        stream.seekg(0, ifstream::beg);
        char* buffer = new char[_align4(length)];
        stream.read(buffer, length);
        data.ptr = buffer;
        data.length = length;
    }
    else
    {
        cout << "read content failed:" << path << endl;
    }

    stream.close();

    return data;
}

void freeData(DATA& data)
{
    delete[] data.ptr;
    data.ptr = NULL;
}

/*
 * 添加自己的压缩算法
 */
#define CHUNK 16384
DATA decompress(const DATA& data)
{
    DATA result = { NULL, 0 };
    return result;
}

DATA compress(const DATA& data)
{
    /*没有压缩*/
    DATA result = { NULL, 0 };
#if 0
    int flush = Z_NO_FLUSH;
    unsigned have = 0;
    z_stream strm;

    /* allocate deflate state */
    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
    strm.opaque = Z_NULL;

    int ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
    if (ret != Z_OK)
    {
        return result;
    }
    char* ptr = new char[data.length];

    /* compress until end of file */
    int rest = data.length;
    int avial_in = (rest > CHUNK ? CHUNK : rest);

    char* in = data.ptr;
    char* out = ptr;

    do
    {
        strm.avail_in = avial_in;
        if (rest < 0)
        {
            (void) deflateEnd(&strm);
            return result;
        }
        flush = (rest == 0) ? Z_FINISH : Z_NO_FLUSH;
        strm.next_in = (unsigned char*)in;

        /* run deflate() on input until output buffer not full, finish
         compression if all of source has been read in */
        do
        {
            strm.avail_out = CHUNK;
            strm.next_out = (unsigned char*) out;
            ret = deflate(&strm, flush); /* no bad return value */
            assert(ret != Z_STREAM_ERROR);
            /* state not clobbered */
            have = CHUNK - strm.avail_out;
            out += have;
            /*if (fwrite(out, 1, have, dest) != have || ferror(dest))
             {
             (void) deflateEnd(&strm);
             return Z_ERRNO;
             }*/
        }
        while (strm.avail_out == 0);
        /* all input will be used */
        assert(strm.avail_in == 0);

        rest -= avial_in;
        avial_in = (rest > CHUNK ? CHUNK : rest);
        /* done when last data in file processed */
    }
    while (flush != Z_FINISH);assert(ret == Z_STREAM_END);
    /* stream will be complete */

    /* clean up and return */
    deflateEnd(&strm);
    result.length = (out - result.ptr );
#endif
    return result;
}
/*
 * 添加自己的加密算法
 */
DATA encrypt(const DATA& data)
{
    DATA result = { NULL, 0 };
    /*进行简单的逆转加密*/
    result.ptr = new char[_align4(data.length)];
    result.length = data.length;
    for (unsigned int index = 0; index < data.length; index++)
    {
        result.ptr[index] = data.ptr[data.length - 1 - index];
    }
    return result;
}

DATA decrypt(const DATA& data)
{
    DATA result = { NULL, 0 };
    /*进行简单的逆转解密*/
    result.ptr = new char[_align4(data.length)];
    result.length = data.length;
    for (unsigned int index = 0; index < data.length; index++)
    {
        result.ptr[index] = data.ptr[data.length - 1 - index];
    }
    return result;
}
